package service;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import sensorgui.Grid;
import sensorgui.SensableObject;
import sensorgui.Sensor;

import com.agentfactory.platform.core.IAgent;
import com.agentfactory.logic.lang.FOS;
import com.agentfactory.platform.impl.AbstractPlatformService;


public class SensorNetPlatformService extends AbstractPlatformService implements PropertyChangeListener{

	protected Grid sensorGrid;
	protected List<Sensor> unassignedSensors = new ArrayList<Sensor>();
	protected List<SensableObject> unassignedSensable = new ArrayList<SensableObject>();
	
	protected Map<String, List<FOS>> mapAgentToPercepts = new HashMap<String, List<FOS>>();
	
	protected Map<String, Sensor> mapAgentToSensor = new HashMap<String, Sensor>();
    protected Map<Sensor, String> mapSensorToAgent = new HashMap<Sensor, String>();
    
	
	public SensorNetPlatformService() {
		// TODO Auto-generated constructor stub
		sensorGrid = new Grid(4,2);
		unassignedSensors = sensorGrid.getSensors();
		
		sensorGrid.show();
	}
	
	public void onStart() {} 
 
    public void onBind(IAgent agent) {
    	if (!unassignedSensors.isEmpty()) {
            //unassignedSensors.get(0).addPropertyChangeListener(this);
            mapSensorToAgent.put(unassignedSensors.get(0), agent.getName());
            mapAgentToSensor.put(agent.getName(), unassignedSensors.remove(0));
            mapAgentToPercepts.put(agent.getName(), new LinkedList<FOS>());
        } else {
            mapAgentToSensor.put(agent.getName(), null);
        }
        System.out.println("Bound to service");
        updatePercepts(agent.getName());
    	
    } 
 
    public void onUnbind(IAgent agent) {} 
 
    public void onStop() {}
    
    public void updatePercepts(String agent) {
    	List<FOS> percepts = mapAgentToPercepts.get(agent);
    	percepts.clear();

    	Sensor sens = lookupSensor(agent);
    	// Agents that are not sensors cannot have any percepts
    	if (sens == null) return;

    	//sensorGrid.
    	
        percepts.add(new FOS("location(" + sens.getX() + "," + sens.getY() + ")"));
        percepts.add(new FOS("direction(" + sens.getDirection().toName() + ")"));
    	
    	
    	
//    	if (perceptSquare.getSquare().isOccupied())
//    			percepts.add(new FOS("occupied(" + squareName + ")"));
//    		
    }
    
    public List<FOS> getPercepts(IAgent agent) {
        return mapAgentToPercepts.get(agent.getName());
    }
    
    public List<FOS> getPercepts(String agent) {
        return mapAgentToPercepts.get(agent);
    }
    
    public void propertyChange(PropertyChangeEvent evt) {
    	System.out.println(evt.getPropertyName() + " SNService");
    }
    
    public Sensor lookupSensor(String agent) {
    	return mapAgentToSensor.get(agent);
    }

    public String lookupAgent(Sensor sensor) {
    	return mapSensorToAgent.get(sensor);
    }

}
